package dao;
import tool.OtherToolClass;
import tool.RowMapper;
import tool.BaseDao;
import java.util.List;
import java.sql.SQLException;
import java.sql.ResultSet;
import entity.ColortbEntity;
import dao.mapper.ColortbDAOI;

/**
 * 实现颜色表的sql操作
 *@author JiangFan
 */
public class ColortbDAO extends BaseDao implements ColortbDAOI {

	OtherToolClass ot=new OtherToolClass ();
	/**
	 * @param color_name
	 *         根据颜色名查询颜色列表是否存在该颜色
	 * @return 要是出问题了注意查看返回值,查询单行的bsedao有更改
	 */
	@Override
	public ColortbEntity selectColorOne (String color_name) throws SQLException, ClassNotFoundException {
		String sql="select *from colortb where color_name=?";
		Object[] param={color_name};
		//调用查询单个方法
		return this.excuteQueryOne (sql, param,new ColortbRowMapper () );
	}

	/**
	 * @return
	 */
	@Override
	public List <ColortbEntity> selectColorAll ()
			throws ClassNotFoundException, SQLException {
		String sql="select *from colortb";
		//调用查询方法
		return this.executeQuery (sql,null,new ColortbRowMapper ());
	}

	/**
	 * @param ce
	 *         在颜色列表添加颜色值
	 * @return
	 */
	@Override
	public int insertColor (ColortbEntity ce) throws SQLException, ClassNotFoundException {
		String sql="insert into colortb values(?,?)";
		//将自动生成的编号传给实体类
		ce.setColor_id (getID ());
		//将编号和颜色名传入sql语句
		Object[] param={ce.getColor_id (),ce.getColor_name ()};
		//调用添加方法
		return this.update (sql,param);
	}

	/** 自动生成id
	 * @return
	 */
	private String getID () throws SQLException, ClassNotFoundException {
		String sql="select color_id from colortb";
		return ot.getIDtwo (sql,"col",new ColortbRowMapper ());
	}
}
class ColortbRowMapper implements RowMapper<ColortbEntity>{
	@Override
	public ColortbEntity rowmap(ResultSet set) throws  SQLException{
		return new ColortbEntity(
				set.getString("color_id"),
				set.getString("color_name")
		);
	}
}
